﻿/*---------------------------------------------------------------------*/
// fileName="DirectoryRes.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

namespace SureKAM.BackUp.Notify.DataAccess.implement
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SureKAM.BackUp.Notify.Domain;

    /// <summary>
    /// 目录数据处理类
    /// </summary>
    public class DirectoryRes:IDirectory
    {
        #region 私有字段
        private List<Domain.Directory> directories;
        private XMLHelper xmlHelper;
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public DirectoryRes()
        {
            this.xmlHelper = new XMLHelper(AppDomain.CurrentDomain.BaseDirectory + "ConfigFile\\Directory.xml");
            this.directories = this.xmlHelper.GetNodes<Domain.Directory>();
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 添加一个目录对象
        /// </summary>
        /// <param name="directory">目录</param>
        /// <returns>返回目录ID</returns>
        public string InsertDirectory(Domain.Directory directory)
        {
            string objectValue = null;
            try
            {
                this.directories.Add(directory);
                objectValue = directory.ID;
            }
            catch (Exception)
            {
                objectValue = null;
            }
            return objectValue;
        }

        /// <summary>
        /// 获取得有的Directorys对象
        /// </summary>
        /// <returns></returns>
        public List<Domain.Directory> GetDirectorys()
        {
            return this.directories;
        }

        /// <summary>
        /// 更新目录对象
        /// </summary>
        /// <param name="directory">目录对象</param>
        /// <returns>返回更新的修改对象</returns>
        public Domain.Directory UpdateDirectory(Domain.Directory directory)
        {
            for (int i = 0; i < this.directories.Count; i++)
            {
                if(this.directories[i].ID==directory.ID)
                {
                    this.directories[i].Name = (string.IsNullOrEmpty(directory.Name)?this.directories[i].Name:directory.Name);
                    this.directories[i].LastChangeTime = (DateTime.Compare(this.directories[i].LastChangeTime, directory.LastChangeTime) > 0 ? this.directories[i].LastChangeTime : directory.LastChangeTime);
                    this.directories[i].MaxSize = (directory.MaxSize>0?directory.MaxSize:this.directories[i].MaxSize);
                    this.directories[i].Notifys = (directory.Notifys!=null?directory.Notifys:this.directories[i].Notifys);
                    this.directories[i].RealSize = (directory.RealSize>0?directory.RealSize:this.directories[i].RealSize);
                    this.directories[i].Template = (directory.Template!=null?directory.Template:this.directories[i].Template);
                    this.directories[i].Address = (string.IsNullOrEmpty(directory.Address)?this.directories[i].Address:directory.Address);
                    this.directories[i].BackUpServer = (string.IsNullOrEmpty(directory.BackUpServer)?this.directories[i].BackUpServer:directory.BackUpServer);
                    //this.directories[i].CreateTime = directory.CreateTime;
                }
            }
            return directory;
        }

        /// <summary>
        /// 根据ID查询相关对象
        /// </summary>
        /// <param name="id">ID值</param>
        /// <returns>返回相应的目录对象</returns>
        public Domain.Directory GetDirectiryById(string id)
        {
            foreach (Domain.Directory  singleDirecory in this.directories)
            {
                if(singleDirecory.ID==id)
                {
                    return singleDirecory;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据ID删除Directory对象
        /// </summary>
        /// <param name="id">ID值</param>
        /// <returns>返回对象的ID值</returns>
        public string DeleteDirectoryById(string id)
        {
            Domain.Directory directory = GetDirectiryById(id);
            if(directory!=null)
            {
                this.directories.Remove(directory);
                return directory.ID;
            }
            return string.Empty;
        }

        ////将GetNotify放在此处是职责不明！
        /// <summary>
        /// 获取相应目录的通知对象
        /// </summary>
        /// <param name="id">id值</param>
        /// <returns>返回相关的通知者</returns>
        public List<Domain.Notify> GetNotifys(string id)
        {
            List<Domain.DirectoryNotifyMapping> directoryNotifyMappings=null;
            List<Domain.Notify> notifies=new List<Domain.Notify>();
            NotifyRes notifyRes=new NotifyRes();
            XMLHelper xmlHelper = new XMLHelper(AppDomain.CurrentDomain.BaseDirectory + "ConfigFile\\DirectoryNotifyMaping.xml");
            directoryNotifyMappings = xmlHelper.GetNodes<Domain.DirectoryNotifyMapping>();
            if(directoryNotifyMappings!=null)
            {
                directoryNotifyMappings = (from directoryNotifyMapping in directoryNotifyMappings
                                           where directoryNotifyMapping.DirectoryId == id
                                           select directoryNotifyMapping).ToList();
            }
            foreach (DirectoryNotifyMapping mapping in directoryNotifyMappings)
            {
                   if(notifyRes.GetNotifyById(mapping.NotifyId)!=null) notifies.Add(notifyRes.GetNotifyById(mapping.NotifyId));
            }
            return (notifies.Count > 0 ? notifies : null);
        }

        /// <summary>
        /// 获取目录的邮件模板
        /// </summary>
        /// <returns>返回模板对象</returns>
        public Domain.ObjectValue.SendTemplate GetTemplate()
        {
            List<Domain.ObjectValue.SendTemplate> sendTemplates;
            XMLHelper xmlHelper = new XMLHelper(AppDomain.CurrentDomain.BaseDirectory + "ConfigFile\\MailTemplate.xml");
            sendTemplates = xmlHelper.GetNodes<Domain.ObjectValue.SendTemplate>();
            return (sendTemplates != null ? sendTemplates[0] : null);
        }

        /// <summary>
        /// 添存文件
        /// </summary>
        /// <returns></returns>
        public bool SaveToFile()
        {
            return this.xmlHelper.SaveToFile<Domain.Directory>(this.directories);
        }
        #endregion
    }
}
